package com.longe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longe.common.Result;
import com.longe.dao.FriendRequestMapper;
import com.longe.entity.Friend;
import com.longe.entity.FriendRequest;
import com.longe.entity.User;
import com.longe.entity.vo.FriendRequestVO;
import com.longe.service.FriendRequestService;
import com.longe.service.FriendService;
import com.longe.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author longe
 * @since 2020-05-23
 */
@Service
public class FriendRequestServiceImpl extends ServiceImpl<FriendRequestMapper, FriendRequest> implements FriendRequestService {

    @Autowired
    private UserService userService;

    @Autowired
    private FriendService friendService;

    @Override
    public Result<Object> friendRequest(FriendRequest friendReq) {

        LambdaQueryWrapper<FriendRequest> query = new LambdaQueryWrapper<>();
        query.eq(FriendRequest::getFromUser, friendReq.getFromUser())
                .eq(FriendRequest::getToUser, friendReq.getToUser());

        FriendRequest exist = this.getOne(query);
        if(null != exist && (exist.getStatus() == 0 || exist.getStatus() == 2)){
            return new Result<>(false, "你已经发送过好友申请", null);
        }
        if(null != exist && exist.getStatus() == 1){
            return new Result<>(false, "该用户已经是你好友了", null);
        }

        friendReq.setCreateTime(new Date());
        boolean bool = this.save(friendReq);
        if(!bool){
            return new Result<>(false, "发送请求失败", null);
        }
        return new Result<>(true, "发送请求成功", null);
    }

    @Override
    public Result<List<FriendRequestVO>> listFriendRequest(Integer userId) {

        QueryWrapper<FriendRequest> queryWrapper = new QueryWrapper();
        queryWrapper.eq("to_user", userId).eq("status", 0);
        List<FriendRequest> list = this.list(queryWrapper);

        List<FriendRequestVO> result = new ArrayList<>();
        Collection<Integer> fromIds = new ArrayList<>();
        Map<Integer, FriendRequestVO> map = new HashMap<>();
        list.forEach(friendRequest -> {
            FriendRequestVO friendRequestVO = new FriendRequestVO();
            friendRequestVO.setId(friendRequest.getId());
            result.add(friendRequestVO);
            fromIds.add(friendRequest.getFromUser());
            map.put(friendRequest.getFromUser(), friendRequestVO);
        });

        if(!fromIds.isEmpty()){
            QueryWrapper<User> query = new QueryWrapper();
            query.in("id", fromIds);
            List<User> userList = userService.list(query);

            userList.forEach(user -> {
                FriendRequestVO friendRequestVO = map.get(user.getId());
                friendRequestVO.setNickname(user.getNickname()).setPicSmall(user.getPicSmall());
            });
        }

        return new Result<>(true, "请求好友请求列表成功", result);
    }

    @Override
    public Result<Object> friendRequestAccept(Integer reqId) {

        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setStatus(1).setId(reqId);

        boolean bool = this.updateById(friendRequest);
        if(!bool){
            return new Result<>(false, "操作失败", null);
        }

        FriendRequest request = this.getById(reqId);

        Collection<Friend> list = new ArrayList<>();
        Friend friend = new Friend();
        friend.setUserId(request.getToUser()).setFriendId(request.getFromUser()).setCreateTime(new Date());
        list.add(friend);
        Friend friend2 = new Friend();
        friend2.setUserId(request.getFromUser()).setFriendId(request.getToUser()).setCreateTime(new Date());
        list.add(friend2);

        friendService.saveBatch(list);

        return new Result<>(true, "操作成功", null);
    }

    @Override
    public Result<Object> friendRequestReject(Integer reqId) {

        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setStatus(2).setId(reqId);

        boolean bool = this.updateById(friendRequest);
        if(!bool){
            return new Result<>(false, "操作失败", null);
        }
        return new Result<>(true, "操作成功", null);
    }

}
